import 'package:whale/helper/utils/util.dart';

import 'im.dart';

enum ImMsgType {
  text,
  image,
  voice,
  video,
  map,
  card,
  file,
  system,
}

class ImMsgModel {
  Map<String, dynamic> json;
  late ImMsgType type;

  static ImMsgType _getMsgType(Map<String, dynamic> json) {
    String type = json['type'].toString();
    switch (type) {
      case 'text':
        return ImMsgType.text;
      case 'voice':
        return ImMsgType.voice;
      case 'image':
        return ImMsgType.image;
      case 'video':
        return ImMsgType.video;
      case 'map':
        return ImMsgType.map;
      case 'system':
        return ImMsgType.system;
      case 'card':
        return ImMsgType.card;
      case 'file':
        return ImMsgType.file;
      default:
        return ImMsgType.text;
    }
  }

  String getTypeValue(ImMsgType type) {
    switch (type) {
      case ImMsgType.text:
        return 'text';
      case ImMsgType.image:
        return 'image';
      case ImMsgType.voice:
        return 'voice';
      case ImMsgType.video:
        return 'video';
      case ImMsgType.map:
        return 'map';
      case ImMsgType.card:
        return 'card';
      case ImMsgType.file:
        return 'file';
      case ImMsgType.system:
        return 'system';
    }
  }

  late IMMsgStatus status;

  IMMsgStatus _getMsgStatus(Map<String, dynamic> json) {
    String status = json['status'].toString();
    switch (status) {
      case '0':
        return IMMsgStatus.sending;
      case '1':
        return IMMsgStatus.success;
      case '-1':
        return IMMsgStatus.fail;
      default:
        return IMMsgStatus.fail;
    }
  }

  String getStatusValue(IMMsgStatus status) {
    switch (status) {
      case IMMsgStatus.sending:
        return '0';
      case IMMsgStatus.success:
        return '1';
      case IMMsgStatus.fail:
        return '-1';
    }
  }

  String get content {
    return json['content'].toString();
  }

  String get time {
    return json['time'].toString();
  }

  late bool isRead;

  bool get isRemind {
    return json['isRemind'].toString() == '1';
  }

  late String userId;
  late String id;

  ImMsgModel({
    required this.json,
  }) {
    id = json['id'].toString();
    isRead = json['isRead'].toString() == '1';
    userId = json['userId'].toString();
    status = _getMsgStatus(json);
    type = ImMsgModel._getMsgType(json);
  }
  factory ImMsgModel.fromJson(Map<String, dynamic> json) {
    var type = _getMsgType(json);
    switch (type) {
      case ImMsgType.voice:
        return ImMsgVoiceModel(json: json);
      case ImMsgType.file:
        return IMMsgFileModel(json: json);
      case ImMsgType.card:
        return IMMsgCardModel(json: json);
      case ImMsgType.video:
        return IMMsgVideoModel(json: json);
      case ImMsgType.map:
        return ImMsgMapModel(json: json);
      case ImMsgType.image:
        return ImMsgImageModel(json: json);
      default:
        return ImMsgModel(json: json);
    }
  }

  Map<String, dynamic> toJson() => {
        'content': content,
      };
}

class ImMsgImageModel extends ImMsgModel {
  late double w;
  late double h;
  late String thumb;
  ImMsgImageModel({required super.json}) {
    w = numVal(json['w']).toDouble();
    h = numVal(json['h']).toDouble();
    // thumb =  json['thumb'].toString();
    thumb = content;
  }
  @override
  Map<String, dynamic> toJson() => {
        'content': content,
        'thumb': thumb,
        'w': w,
        'h': h,
      };
}

class ImMsgMapModel extends ImMsgModel {
  ImMsgMapModel({required super.json});
}

class ImMsgVoiceModel extends ImMsgModel {
  late int size;
  late int seconds;
  late bool isPlaying;

  ImMsgVoiceModel({required super.json}) {
    size = json['size'] as int? ?? 0;
    seconds = json['seconds'];
    isPlaying = false;
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'content': content,
      'seconds': seconds,
      'size': size,
    };
  }
}

class IMMsgMapModel extends ImMsgModel {
  late double lat;
  late double lng;
  late String title;
  IMMsgMapModel({required super.json}) {
    lat = numVal(json['lat']).toDouble();
    lng = numVal(json['lng']).toDouble();
    title = json['title'].toString();
  }
}

class IMMsgVideoModel extends ImMsgModel {
  late int size;
  late int seconds;

  IMMsgVideoModel({required super.json}) {
    size = json['size'];
    seconds = json['seconds'];
  }
}

class IMMsgCardModel extends ImMsgModel {
  late String title;
  late String des;
  late String image;
  late String sourceIcon;
  late String sourceName;
  IMMsgCardModel({required super.json}) {
    title = json['title'].toString();
    des = json['des'].toString();
    image = json['image'].toString();
    sourceIcon = json['sourceIcon'].toString();
    sourceName = json['sourceName'].toString();
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'title': title,
      'des': des,
      'image': image,
      'sourceIcon': sourceIcon,
      'sourceName': sourceName,
    };
  }
}

class IMMsgFileModel extends ImMsgModel {
  late int size;
  late String title;
  late String format;
  IMMsgFileModel({required super.json}) {
    size = json['size'];
    title = json['title'].toString();
    format = json['format'].toString();
  }
}
